package com.ruoyi.web.controller.system;

import java.util.List;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.SyncTask;
import com.ruoyi.system.service.ISyncTaskService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 数据库同步控制器
 * 
 * @author ruoyi
 */
@RestController
@RequestMapping("/system/dbsync")
public class SyncDbController extends BaseController
{
    @Autowired
    private ISyncTaskService syncTaskService;

    /**
     * 查询数据库同步任务列表
     */
    @PreAuthorize("@ss.hasPermi('system:dbsync:list')")
    @GetMapping("/list")
    public TableDataInfo list(SyncTask syncTask)
    {
        syncTask.setSyncType("db");
        startPage();
        List<SyncTask> list = syncTaskService.selectSyncTaskList(syncTask);
        return getDataTable(list);
    }

    /**
     * 导出数据库同步任务列表
     */
    @PreAuthorize("@ss.hasPermi('system:dbsync:export')")
    @Log(title = "数据库同步", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(SyncTask syncTask)
    {
        syncTask.setSyncType("db");
        List<SyncTask> list = syncTaskService.selectSyncTaskList(syncTask);
        ExcelUtil<SyncTask> util = new ExcelUtil<SyncTask>(SyncTask.class);
        return util.exportExcel(list, "数据库同步");
    }

    /**
     * 获取数据库同步任务详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:dbsync:query')")
    @GetMapping(value = "/{syncId}")
    public AjaxResult getInfo(@PathVariable("syncId") Long syncId)
    {
        return AjaxResult.success(syncTaskService.selectSyncTaskById(syncId));
    }

    /**
     * 新增数据库同步任务
     */
    @PreAuthorize("@ss.hasPermi('system:dbsync:add')")
    @Log(title = "数据库同步", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SyncTask syncTask)
    {
        syncTask.setSyncType("db");
        return toAjax(syncTaskService.insertSyncTask(syncTask));
    }

    /**
     * 修改数据库同步任务
     */
    @PreAuthorize("@ss.hasPermi('system:dbsync:edit')")
    @Log(title = "数据库同步", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SyncTask syncTask)
    {
        return toAjax(syncTaskService.updateSyncTask(syncTask));
    }

    /**
     * 删除数据库同步任务
     */
    @PreAuthorize("@ss.hasPermi('system:dbsync:remove')")
    @Log(title = "数据库同步", businessType = BusinessType.DELETE)
    @DeleteMapping("/{syncIds}")
    public AjaxResult remove(@PathVariable Long[] syncIds)
    {
        return toAjax(syncTaskService.deleteSyncTaskByIds(syncIds));
    }
    
    /**
     * 运行数据库同步任务
     */
    @PreAuthorize("@ss.hasPermi('system:dbsync:run')")
    @Log(title = "数据库同步", businessType = BusinessType.OTHER)
    @GetMapping("/run/{syncId}")
    public AjaxResult run(@PathVariable("syncId") Long syncId)
    {
        syncTaskService.runSyncTask(syncId);
        return AjaxResult.success();
    }
    
    /**
     * 获取数据库列表
     */
    @GetMapping("/dblist/{serverId}")
    public AjaxResult getDbList(@PathVariable("serverId") Long serverId)
    {
        return AjaxResult.success(syncTaskService.getDbList(serverId));
    }
    
    /**
     * 获取数据表列表
     */
    @GetMapping("/tables/{dbId}")
    public AjaxResult getTablesByDb(@PathVariable("dbId") String dbId)
    {
        return AjaxResult.success(syncTaskService.getTablesByDb(dbId));
    }
} 